﻿@using TaskoMask.BuildingBlocks.Contracts.Dtos.Boards
@inject BoardApiService BoardApiService
@inject ProjectApiService ProjectApiService
@inject OrganizationApiService OrganizationApiService

<EditForm Model="BoardModel" OnValidSubmit="HandleUpdateBoard">

    <DataAnnotationsValidator />

    <div class="form-group">
        <InputText @bind-Value="BoardModel.Name" placeholder="Name" class="form-control"></InputText>
        <ValidationMessage For="()=>BoardModel.Name" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <InputText @bind-Value="BoardModel.Description" placeholder="Description" class="form-control"></InputText>
        <ValidationMessage For="()=>BoardModel.Description" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
            <button type="submit" class="btn btn-primary float-left">Update</button>
            <button @onclick="HandleDeleteBoard" class="btn btn-danger text-white float-right">Delete</button>
    </div>

</EditForm>

@code {


    #region Fields and Parameters



    /// <summary>
    /// Get the board to update by its Id
    /// </summary>
    [Parameter]
    public string Id { get; set; }



    /// <summary>
    /// Form Model
    /// </summary>
    UpdateBoardDto BoardModel = new UpdateBoardDto();



    /// <summary>
    /// Access to current modal
    /// </summary>
    [CascadingParameter]
    BlazoredModalInstance ModalInstance { get; set; }




    #endregion Fields and Parameters



    #region Protected Methods


    /// <summary>
    ///
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        await PrepareModelToUpdate();

    }


    #endregion Protected Methods


    #region private Methods



    /// <summary>
    /// Prepare the form model
    /// </summary>
    private async Task PrepareModelToUpdate()
    {

        var boardResult = await BoardApiService.GetAsync(Id);

        if (boardResult.IsSuccess)
        {
            BoardModel = new UpdateBoardDto
                {
                    Id = boardResult.Value.Id,
                    Name = boardResult.Value.Name,
                    Description = boardResult.Value.Description,
                };
        }
        else
        {
            QueryResultHandler<GetBoardDto>.Init()
                .WithResult(boardResult)
                .ShowErrorToast(ToastService)
                .CloseModal(ModalInstance);
        }
    }



    /// <summary>
    /// handle update model
    /// </summary>
    private async Task HandleUpdateBoard()
    {
        var result = await BoardApiService.UpdateAsync(Id, BoardModel);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Board_Updated)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }



    /// <summary>
    /// handle delete item
    /// </summary>
    private async Task HandleDeleteBoard()
    {
        var confirmationModal = ModalService.Show<Confirmation>("Deletion Warning!", new ModalOptions { Class="blazored-modal w-75 w-md-50 bg-light-gray"});
        var confirmationModalResult = await confirmationModal.Result;

        if (confirmationModalResult.Cancelled)
            return;

        var result = await BoardApiService.DeleteAsync(Id);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Board_Deleted)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }



    #endregion private Methods

}